//
//  Channel_ViewController.m
//  NewMyMusicAPP
//
//  Created by dlios on 15/6/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Channel_ViewController.h"

@interface Channel_ViewController ()

@property (retain ,nonatomic) MBProgressHUD *MBP;

@end

@implementation Channel_ViewController

- (void)dealloc
{

    [_Id release];
    [_array release];
    [_myTableView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    
    //       给导航栏上放自定义label
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 100,HEIGHT * 30)]autorelease];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(-MYWIDTH / 2 + WIDTH * 85, 0, WIDTH * 300, HEIGHT * 30)];
    title.text = self.title;
   // view.center = title.center;
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    
    UIImage *image1 = [UIImage imageNamed:@"VList_Left@2x.png"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image1 style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    
    
    

    self.myTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH, MYHEIGHT - 64) style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self asker];
    
    self.myTableView.showsVerticalScrollIndicator = NO;//取消纵向滑条
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell之间的线
    
    //动态小黄人
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    
     self.number = 0;
    self.array = [NSMutableArray array];
    [self addHeader];
    [self addfooder];
   
}

- (void)back
{
    [GiFHUD dismiss];

    [self.navigationController popViewControllerAnimated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;
    }else
    {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"cell";
    Channel_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[Channel_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
    cell.backgroundColor = [UIColor clearColor];
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    

    if (self.array.count != 0) {
        Channel_Model *channel = [self.array objectAtIndex:indexPath.row];
        
        
        cell.channelM = [self.array objectAtIndex:indexPath.row];
        [[DataHandle shareHandleController] openDB];
        [[DataHandle shareHandleController] createCollectTable];
        NSMutableArray *array = [[DataHandle shareHandleController] selectCollect];
        
        if (array.count == 0) {
            cell.buer = NO;
            
        }else
        {
            for (CollectModel *collect in array) {
                
                NSString *modelId = [NSString stringWithFormat:@"%@",channel.Id];
                if ([collect.modelId isEqualToString:modelId]) {
                    cell.buer  = YES;
                    cell.channelM = [self.array objectAtIndex:indexPath.row];
                    
                    return cell;
                }
            }
            cell.buer = NO;
            cell.channelM = [self.array objectAtIndex:indexPath.row];
            
        }
    }


    
   
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 110;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoPlayerViewController *videoPlayerVC = [[[VideoPlayerViewController alloc] init]autorelease];
    if (self.array.count != 0) {
        Channel_Model *channelM = [self.array objectAtIndex:indexPath.row];
        videoPlayerVC.Id = channelM.Id;
        videoPlayerVC.type = @"VIDEO";
        videoPlayerVC.title = channelM.title;
    }
    
    
    [self.navigationController pushViewController:videoPlayerVC animated:YES];
    
}

//下拉刷新
-(void)addHeader
{
    __block Channel_ViewController *channel = self;
    
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [channel asker];
        
    }];
    
    //一进程序自动刷新
    //[self.myTableView.header beginRefreshing];
}
//上啦刷新
-(void)addfooder
{
    __block Channel_ViewController *channel = self;
   
    self.myTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.number < 40) {
            self.number += 20;
            [channel asker];
        }else
        {
            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经到底了" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [self.myTableView.footer endRefreshing];//结束上拉刷新

            [aler show];
        }
        
        
        
    }];
    
    //一进程序自动刷新
    //[self.myTableView.header beginRefreshing];
}



- (void)asker
{

    NSMutableDictionary *askerDic = [NSMutableDictionary dictionary];

    
    NSString *num = [NSString stringWithFormat:@"%ld",self.number];
    [askerDic setObject:@"0" forKey:@"D-A"];
    [askerDic setObject:self.Id forKey:@"channelId"];
    [askerDic setObject:@"true" forKey:@"detail"];
    [askerDic setObject:num forKey:@"offset"];
    [askerDic setObject:@"20" forKey:@"size"];
    [askerDic setObject:@"VideoPubDate" forKey:@"order"];
    
    
    [Connect ConnectRequestAFWithURL:ChannelDetail params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {

        NSMutableDictionary *resultDic = (NSMutableDictionary *)result;
        NSMutableArray *array = [resultDic objectForKey:@"videos"];
        
        for (NSMutableDictionary *dic in array) {
            Channel_Model *channelM = [[[Channel_Model alloc] init]autorelease];
            channelM.title = [dic objectForKey:@"title"];
            channelM.artistName = [dic objectForKey:@"artistName"];
            channelM.playListPic = [dic objectForKey:@"playListPic"];
            channelM.Id = [dic objectForKey:@"id"];

            [self.array addObject:channelM];
        }
        
        [GiFHUD dismiss];
        [self.myTableView reloadData];
        [self.myTableView.header endRefreshing];//结束下拉刷新
        [self.myTableView.footer endRefreshing];//结束上拉刷新
    }];
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
